package cn.hrc.controller.adm;

import cn.hrc.entity.Type;
import cn.hrc.entity.TypeInfo;
import cn.hrc.entity.XJson;
import cn.hrc.service.TypeService;
import cn.hrc.service.impl.TypeServiceImpl;
import cn.hrc.utils.PackJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/adm/type")
public class TypeController {
    @Autowired
    TypeServiceImpl typeService;
    @RequestMapping("/findAll")
    public XJson findAll() {
        List<TypeInfo> types = typeService.findAll2();
        return PackJson.pack(200, "ok", types);
    }
    @RequestMapping("/findAll2")
    public XJson findAll2() {
        List<Type> types = typeService.findAll();
        return PackJson.pack(200, "ok", types);
    }
    @GetMapping("/findById/{id}")
    public XJson findById(@PathVariable("id") int id) {
        //
        Type type = typeService.findById(id);
        //
        Type type2= typeService.findById(type.getPid());
        //
        Map<String,Object> data = new HashMap<>();
        data.put("type",type);
        data.put("type2",type2);
        return PackJson.pack(200, "ok", data);
    }
    @RequestMapping("/addType")
    // @RequestBody 获取json数据
    public XJson addType(@RequestBody Type type) {
        typeService.addType(type);
        List<TypeInfo> types = typeService.findAll2();
        return PackJson.pack(200, "ok", types);
    }
    @RequestMapping("/updateType")
    public XJson updateType(@RequestBody Type type) {
        typeService.updateType(type);
        List<TypeInfo> types = typeService.findAll2();
        return PackJson.pack(200, "ok", types);
    }
}
